package org.ejde.sciencelogs;

import java.awt.Dimension;
import java.util.Collection;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

/**
 * This class contains the attributes and behaviors for 
 * JComponents related to XMPP-base client roster list.
 *  
 * @author Eric Martin
 * @version 0.1
 */
public class smackUserList extends JTabbedPane implements RosterListener,
		ConnectionListener, ConnectionCreationListener, MessageListener {

	// Creating a Tab panel for the online users.
	private JScrollPane onlineusers;
	private JTree onlineusertree;
	private DefaultMutableTreeNode onlineroot;

	// Creating a Tab panel for the online users.
	private JScrollPane offlineusers;
	private JTree offlineusertree;
	private DefaultMutableTreeNode offlineroot;

	// The XMPP-client.
	private smackClient smack;

	// The user roster for the XMPP client.
	private Roster roster;

	/**
	 * Constructor method.  Initializes the JComponents.
	 * @param ini
	 * @param smackclient
	 */
	smackUserList(iniParser ini, smackClient smackclient) {

		this.smack = smackclient;

		// initiate the user trees
		
		// the list of online users.
		this.onlineroot = new DefaultMutableTreeNode("Online");
		this.onlineusertree = new JTree(onlineroot);
		this.onlineusertree.setRootVisible(false);
		this.onlineusers = new JScrollPane(onlineusertree);
		this.onlineusers.setPreferredSize(new Dimension(250, 400));

		// the list of the offline users.
		this.offlineroot = new DefaultMutableTreeNode("Offline");
		this.offlineusertree = new JTree(offlineroot);
		this.offlineusertree.setRootVisible(false);
		this.offlineusers = new JScrollPane(offlineusertree);
		this.offlineusers.setPreferredSize(new Dimension(250, 400));

		// add the scroll panels to the tabbed pane.
		this.addTab("Online", onlineusers);
		this.addTab("Offline", offlineusers);

		// Retrive the roster list from the XMPP server.
		this.populateRoster();

		// bind the roster object to respond to roster
		// changes on the XMPP server.
		this.roster.addRosterListener(this);

		// Refresh the JComponents.
		this.repaintAll();

	}

	/**
	 * Get the username based on the roster entry object.
	 * 
	 * @param entry the entry object to extract the
	 * username from.
	 * 
	 * @return the entry name as a string.
	 */
	private String getListName(RosterEntry entry) {
		String output = entry.getName();
		return output;
	}

	/**
	 * Populate the online and offline lists and refresh
	 * the JComponents.
	 */
	public void populateRoster() {
		// Get the roster
		this.roster = this.smack.getRoster();

		this.updateList();
		this.repaintAll();
	}

	/**
	 * Populate the online and offline lists.
	 */
	public void updateList() {
		Collection<RosterEntry> entries = this.roster.getEntries();

		// System.out.println("-----GET USERS------");
		//Clean out the users currently listed in the JTrees.
		this.onlineroot.removeAllChildren();
		this.offlineroot.removeAllChildren();

		
		for (RosterEntry entry : entries) {
			// System.out.println(roster.getPresence(entry.getUser()));
			// check presence
			Presence status = this.roster.getPresence(entry.getUser());
			DefaultMutableTreeNode usernode = new DefaultMutableTreeNode(
					getListName(entry));
			if (status != null & status.getType() == Presence.Type.available) {
				// System.out.println("AVAILABLE user");
				this.onlineroot.add(usernode);
			} else if (status != null
					& status.getType() == Presence.Type.unavailable) {
				// System.out.println("offline user");
				this.offlineroot.add(usernode);
			}
		}

	}

	/**
	 * Refresh all the JComponents.
	 */
	private void repaintAll() {
		this.onlineusertree.updateUI();
		this.onlineusertree.repaint();

		this.offlineusertree.updateUI();
		this.offlineusertree.repaint();

		this.onlineusers.updateUI();
		this.onlineusers.repaint();

		this.offlineusers.updateUI();
		this.offlineusers.repaint();

		this.repaint();
	}

	public void entriesAdded(Collection<String> arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void entriesDeleted(Collection<String> arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void entriesUpdated(Collection<String> arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void presenceChanged(Presence arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void connectionClosed() {
		this.updateList();
		this.repaintAll();

	}

	public void connectionClosedOnError(Exception arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void reconnectingIn(int arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void reconnectionFailed(Exception arg0) {
		this.updateList();
		this.repaintAll();

	}

	public void reconnectionSuccessful() {
		this.updateList();
		this.repaintAll();

	}

	public void connectionCreated(XMPPConnection arg0) {
		this.updateList();
		this.repaintAll();
	}

	public void processMessage(Chat arg0, Message arg1) {
		this.updateList();
		this.repaintAll();

	}

}
